package com.sugon.stream;

import com.sugon.entity.Employees;

import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

/**
 * 多个中间操作可以连接起来形成一个流水线，除非流水 线上触发终止操作，否则中间操作不会执行任何的处理！
 * 而在终止操作时一次性全部处理，称为“惰性求值”。
 */
public class TestStream_group {
    public static void main(String[] args) {
        List<Employees> list = Employees.createEmployee();

        /**
         * 按部门分组
         */
        Map<String, List<Employees>> collect = list.stream().collect(Collectors.groupingBy(Employees::getDept));

        collect.forEach((s, l) -> {
            System.out.println(s);
            l.forEach(System.out::println);
        });


        /**
         * 按本分组求部门平均薪水
         */
        System.out.println("-------------------------------------------------------------------------");
        Map<String, Double> collect1 = list.stream().collect(Collectors.groupingBy(Employees::getDept, Collectors.averagingDouble(Employees::getSalary)));
        collect1.forEach((s,l)->{
            System.out.println(s+":"+l);
        });


        System.out.println("===========================================================================");
        /**
         * 按部门分组统计个数
         */
        Map<String, Long> c = list.stream().collect(Collectors.groupingBy(Employees::getDept, Collectors.counting()));
        c.forEach((s, l) -> {
            System.out.println(s + ":" + l);
        });
    }


}

